/* Name: Mouse
 * Author: John Gideon
 * Date: 5-3-12
 * Description: Responds to mouse presses and releases and stores them as temporary flags
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.awt.Canvas;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Mouse implements MouseListener {
	
	private static int x = 0;
	private static int y = 0;
	private static Canvas canvas;
	private static Boolean leftPress = false;
	private static Boolean rightPress = false;
	private static Boolean leftRelease = false;
	private static Boolean rightRelease = false;
	
	// Attach a mouse to a window for input
	public Mouse(Canvas pCanvas) {
		canvas = pCanvas;
	}

	// Sets a flag when a mouse button is pressed
	public void mousePressed(MouseEvent e) {
		if (e.getButton()==MouseEvent.BUTTON1) {
			leftPress = true;
		} else if (e.getButton()==MouseEvent.BUTTON3) {
			rightPress = true;
		}
	}

	// Sets a flag when a mouse button is released
	public void mouseReleased(MouseEvent e) {
		if (e.getButton()==MouseEvent.BUTTON1) {
			leftRelease = true;
		} else if (e.getButton()==MouseEvent.BUTTON3) {
			rightRelease = true;
		}
	}
	
	// Updates the variable holding the positions of the mouse so that all calls during the same frame are consistent
	public static void updateLocation() {
		Point mousePos = canvas.getMousePosition();
		if (mousePos == null) {
			x = 999999;
			y = 999999;
		} else {
			x = mousePos.x;
			y = mousePos.y;
		}
	}
	
	// Position queries
	public static int getX() {return x;}
	public static int getY() {return y;}
	
	// Returns and resets the leftPress flag
	public static Boolean getLeftPressed() {
		Boolean tmp = leftPress;
		leftPress = false;
		return tmp;
	}
	
	// Returns and resets the rightPress flag
	public static Boolean getRightPressed() {
		Boolean tmp = rightPress;
		rightPress = false;
		return tmp;
	}
	
	// Returns and resets the leftRelease flag
	public static Boolean getLeftReleased() {
		Boolean tmp = leftRelease;
		leftRelease = false;
		return tmp;
	}
	
	// Returns and resets the rightRelease flag
	public static Boolean getRightReleased() {
		Boolean tmp = rightRelease;
		rightRelease = false;
		return tmp;
	}
	
	// Unused callbacks
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	
}
